<?php

namespace app\admin\controller\business;

use app\common\controller\Backend;

/**
 * 
 */
class Privateseainfo extends Backend
{
    /**
     * 当前控制器下的一个模型属性
     */
    protected $model = null;

    // 初始化
    public function _initialize()
    {
        parent::_initialize();

        // 全局用户模型
        $this->BusinessModel = model('Business.Business');

        // 回访表
        $this->VisitModel = model('Business.Visit');

        $this->AdminModel = model('Admin');

        $this->ReceiveModel = model('Business.Receive');

        $this->SourceModel = model('Business.Source');

        $this->RegionModel = model('Region');

        $this->Adminlist = $this->AdminModel->select();

        $Admindata = [];

        foreach ($this->Adminlist as $k => &$v) {

            $Admindata[$v['id']] = $v['username'];
        }

        $sourclist = $this->SourceModel->select();

        $Sourcdata = [];

        foreach ($sourclist as $val) {

            $Sourcdata[$val['id']] = $val['name'];
        }

        $Sexlist = [0 => '保密', 1 => '男', 2 => '女'];

        $Deallist = [0 => '未成交', 1 => '已成交'];

        $this->view->assign([
            'Admindata' =>  $Admindata,
            'Sourcdata' =>  $Sourcdata,
            'Sexlist' => $Sexlist,
            'Deallist' => $Deallist
        ]);
    }

    // 回访列表
    public function index($ids = null)
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);

        if ($this->request->isAjax()) {

            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            // 此处将筛选请求中有争议的字段"id"指定为tableA中的id
            $this->changeWhereName("id", "visit.id");
            $this->changeWhereName("createtime", "visit.id");

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->VisitModel
                ->with(['admin', 'business'])
                ->where($where)
                ->where('busid', $ids)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count();  //查询总数

            $list = $this->VisitModel
                ->with(['admin', 'business'])
                ->where($where)
                ->where('busid', $ids)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();  //查询数据

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }

        return $this->view->fetch();
    }

    // 添加回访记录
    public function add($ids = null)
    {
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            $params['busid'] = $ids;
            $params['adminid'] = $this->auth->id;

            if ($params) {
                $result = $this->VisitModel->validate("Common/Business/Visit")->save($params);
                if ($result === false) {
                    $this->error($this->VisitModel->getError());
                    exit;
                }
                $this->success();
                exit;
            }
        }

        return $this->view->fetch();
    }

    // 编辑回访记录
    public function edit($ids = null)
    {
        $row = $this->VisitModel->find($ids);

        if (!$row) {
            $this->error(__('未找到当前回访记录'));
            exit;
        }

        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");

            if ($params) {
                $params['id'] = $ids;
                $params['busid'] = $row['busid'];
                $params['adminid'] = $row['adminid'];
                $result = $this->VisitModel->validate("Common/Business/Visit")->isUpdate(true)->save($params);

                if ($result === false) {
                    $this->error($this->VisitModel->getError());
                    exit;
                }

                $this->success();
                exit;
            }
        }

        $this->assign("row", $row);

        return $this->view->fetch();
    }

    // 删除回访记录
    public function del($ids = null)
    {
        $ids = !empty($ids) ?  explode(',', $ids) : [];

        $row = $this->VisitModel->column('id');

        foreach ($ids as $item) {
            if (!in_array($item, $row)) {
                $this->error(__('没有找到该回访记录'));
                exit;
            }
        }

        $result = $this->VisitModel->destroy($ids);

        if ($result === false) {
            $this->error($this->VisitModel->getError());
            exit;
        }

        $this->success();
        exit;
    }

    // 申请列表
    public function receive($ids = null)
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);

        if ($this->request->isAjax()) {
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            // 此处将筛选请求中有争议的字段"id"指定为tableA中的id
            $this->changeWhereName("id", "receive.id");

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->ReceiveModel
                ->with(['admin', 'business'])
                ->where($where)
                ->where('busid', $ids)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count();  //查询总数

            $list = $this->ReceiveModel
                ->with(['admin', 'business'])
                ->where($where)
                ->where('busid', $ids)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();  //查询数据

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }

        return $this->view->fetch();
    }
}
